<?php


namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\facade\Request;
use app\common\model\Setting as SettingModel;

class Setting extends  Controller
{

    protected $middleware = [
        'app\http\middleware\checkAdminToken::class'
    ];

    //返回指定字段的设置
    public function read()
    {
        $name=Request::param('name');

        $data=Db::table('setting')->where('name',$name)->find();

        $this->result( $data,200,'');
    }
    //返回多个字段
    public function readSettings()
    {
        $names =Request::param('names');
        $data=Db::table('setting')
            ->where('name','in',$names)
            ->select();
        $this->result( $data,200,'请求成功');
    }

    //返回常见问题列表设置
    public function common_problem()
    {

        $data=Db::table('setting')->where('name','common_problem')->value('data');
        if($data){
            $data=json_decode($data);
        }else{
            $data=[];
        }
        $this->result( $data,200,'');
    }

    //设置常见问题
    public function save_common_problem()
    {


        $data  =Request::param('data','[]');

        $res=SettingModel::saveSettings('common_problem',$data);
        $this->result($res,200,'保存成功');
    }

    public function tutorial()
    {
        $data=Db::table('setting')->where('name','tutorial')->value('data');
        if($data){
            $data=json_decode($data);
        }else{
            $data=null;
        }
        $this->result( $data,200,'');
    }

    //设置新手教程
    public function save_tutorial()
    {
        $data=Request::param('data','[]');
        $res=SettingModel::saveSettings('tutorial',$data);
        $this->result($res,200,'保存成功');
    }


    public function index()
    {
        $data=SettingModel::getAllSetting();
        $this->result($data,200,'');
    }

    public function update()
    {
        $setting=Request::only(['logo','notice','about_us','group_chat_qrcode','official_account_qrcode','app_name','wechat','share_title','vip_max_link','user_max_link',
            'content_source_url','is_real','app_id','adv_id','check_version','payment_ratio','upload_mode','poster','lv_1_share_income_money','lv_2_share_income_money',
            'min_take_out_money','video_share_title','high_vip_lv_1_share_income_money','high_vip_lv_2_share_income_money','lv_1_share_type']);


        //设置每个的值
        foreach ($setting as $key => $item) {
            SettingModel::saveSettings($key,$item);
        }

        //设置成功
        $this->result('',200,'设置成功');
    }



}
